#pragma once

#pragma once

#include <iostream>
#include <vector>
#include <string>
#include <assert.h>

using namespace std;

template<class K, class V>
struct AVLTNode
{
	AVLTNode(const pair<K, V>& kv)
		:_left(nullptr)
		, _right(nullptr)
		, _parent(nullptr)
		, _kv(kv)
		, _bf(0)
	{}

	AVLTNode<K, V>* _left;
	AVLTNode<K, V>* _right;
	AVLTNode<K, V>* _parent;
	pair<K, V> _kv;
	int _bf;
};

template<class K, class V>
class AVLTree
{
public:
	typedef AVLTNode<K, V> Node;

	bool insert(const pair<K, V>& kv)
	{
		if (_root == nullptr)
		{
			_root = new Node(kv);
			return true;
		}

		Node* parent = nullptr;
		Node* cur = _root;
		while (cur)
		{
			if (kv.first < cur->_kv.first)
			{
				parent = cur;
				cur = cur->_left;
			}
			else if (kv.first > cur->_kv.first)
			{
				parent = cur;
				cur = cur->_right;
			}
			else return false;
		}

		Node* newnode = new Node(kv);
		newnode->_parent = parent;
		if (kv.first < parent->_kv.first) parent->_left = newnode;
		else parent->_right = newnode;

		cur = newnode;
		// update balance factor
		while (parent)
		{
			if (cur == parent->_left) parent->_bf--;
			else parent->_bf++;

			if (parent->_bf == 0) break;
			else if (parent->_bf == 1 || parent->_bf == -1)
			{
				cur = parent;
				parent = parent->_parent;
			}
			else if (parent->_bf == 2 || parent->_bf == -2)
			{
				if (parent->_bf == -2 && cur->_bf == -1) RotateR(parent);
				else if (parent->_bf == 2 && cur->_bf == 1) RotateL(parent);
				else if (parent->_bf == 2 && cur->_bf == -1) RotateRL(parent);
				else if (parent->_bf == -2 && cur->_bf == 1) RotateLR(parent);
				break;
			}
			else assert(false);
		}

		return true;
	}

	void RotateR(Node* parent)
	{
		Node* cur = parent->_left;
		Node* grandparent = parent->_parent;
		Node* curR = cur->_right;

		parent->_left = curR;
		if (curR) curR->_parent = parent;
		parent->_parent = cur;
		cur->_right = parent;

		cur->_parent = grandparent;
		if (grandparent)
		{
			if (parent == grandparent->_left) grandparent->_left = cur;
			else grandparent->_right = cur;
		}
		else _root = cur;

		cur->_bf = parent->_bf = 0;
	}

	void RotateL(Node* parent)
	{
		Node* cur = parent->_right;
		Node* grandparent = parent->_parent;
		Node* curL = cur->_left;

		parent->_right = curL;
		if (curL) curL->_parent = parent;
		parent->_parent = cur;
		cur->_left = parent;

		cur->_parent = grandparent;
		if (grandparent)
		{
			if (parent == grandparent->_left) grandparent->_left = cur;
			else grandparent->_right = cur;
		}
		else _root = cur;

		cur->_bf = parent->_bf = 0;
	}

	void RotateRL(Node* parent)
	{
		Node* cur = parent->_right;
		Node* curL = cur->_left;

		int bf = curL->_bf;

		RotateR(cur);
		RotateL(parent);

		curL->_bf = 0;
		if (bf == 0)
		{
			cur->_bf = 0;
			parent->_bf = 0;
		}
		else if (bf == -1)
		{
			cur->_bf = 1;
			parent->_bf = 0;
		}
		else if (bf == 1)
		{
			cur->_bf = 0;
			parent->_bf = -1;
		}
	}

	void RotateLR(Node* parent)
	{
		Node* cur = parent->_left;
		Node* curR = cur->_right;

		int bf = curR->_bf;

		RotateL(cur);
		RotateR(parent);

		curR->_bf = 0;
		if (bf == 0)
		{
			cur->_bf = 0;
			parent->_bf = 0;
		}
		else if (bf == -1)
		{
			cur->_bf = 0;
			parent->_bf = 1;
		}
		else if (bf == 1)
		{
			cur->_bf = -1;
			parent->_bf = 0;
		}
	}

	Node* find(const K& key)
	{
		Node* cur = _root;
		while (cur)
		{
			if (key < cur->_key) cur = cur->_left;
			else if (key > cur->_key) cur = cur->_right;
			else return cur;
		}
		return nullptr;
	}

	bool erase(const K& key)
	{
		Node* parent = nullptr;
		Node* cur = _root;
		while (cur)
		{
			if (key < cur->_key)
			{
				parent = cur;
				cur = cur->_left;
			}
			else if (key > cur->_key)
			{
				parent = cur;
				cur = cur->_right;
			}
			else
			{
				if (cur == _root && (cur->_left == nullptr || cur->_right == nullptr))
				{
					if (cur->_left == nullptr)
					{
						_root = cur->_right;
						delete cur;
					}
					else
					{
						_root = cur->_left;
						delete cur;
					}
				}
				else
				{
					if (cur->_left == nullptr)
					{
						if (cur == parent->_left) parent->_left = cur->_right;
						else parent->_right = cur->_right;
						delete cur;
					}
					else if (cur->_right == nullptr)
					{
						if (cur == parent->_left) parent->_left = cur->_left;
						else parent->_right = cur->_left;
						delete cur;
					}
					else
					{
						Node* leftMax = cur->_left;
						Node* leftMaxP = cur;
						while (leftMax->_right)
						{
							leftMaxP = leftMax;
							leftMax = leftMax->_right;
						}
						swap(leftMax->_key, cur->_key);

						if (leftMax == leftMaxP->_right) leftMaxP->_right = leftMax->_left;
						else leftMaxP->_left = leftMax->_left;
						delete leftMax;
					}
				}
				return true;
			}
		}
		return false;
	}

	void InOrder()
	{
		_InOrder(_root);
		cout << endl;
	}

	bool IsBalance()
	{
		return _IsBalance(_root);
	}

	int Height(Node* root)
	{
		if (root == nullptr) return 0;

		return max(Height(root->_left), Height(root->_right)) + 1;
	}

protected:
	bool _IsBalance(Node* root)
	{
		if (root == nullptr) return true;

		int leftHeight = Height(root->_left);
		int rightHeight = Height(root->_right);

		if (abs(leftHeight - rightHeight) >= 2)
		{
			cout << root->_kv.first << "->" << "is not balance" << endl;
			return false;
		}

		if (rightHeight - leftHeight != root->_bf)
		{
			cout << root->_kv.first << "->" << "the balance factor is not right" << endl;
			return false;
		}

		return _IsBalance(root->_left) && _IsBalance(root->_right);
	}

	void _InOrder(const Node* root)
	{
		if (root == nullptr) return;
		_InOrder(root->_left);
		cout << root->_kv.first << ":" << root->_kv.second << endl;
		_InOrder(root->_right);
	}

	Node* _root = nullptr;
};

void Test_AVLTree1()
{
	AVLTree<int, int> tree;
	int a[] = { 677,6432,10946,30693,18146,6432,16760,6432,15124 };
	for (auto& e : a)
	{
		if (e == 16760)
		{
			int i = 0;
		}

		tree.insert({ e,e });
		cout << e << "->" << tree.IsBalance() << endl;
	}
}

void Test_AVLTree2()
{
	int N = 1000000;
	srand(time(nullptr));
	AVLTree<int, int> tree;

	for (int i = 0; i < N; i++)
	{
		int num = rand() + i;
		tree.insert({ num, num });
	}

	cout << tree.IsBalance() << endl;
}